﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using GisControl.CustomMarkers;
using GisControl.MapProviders.BaiduMapProvider;
using GisControl.MapProviders.GaodeMapProvider;
using GMap.NET;
using GMap.NET.WindowsPresentation;

namespace GisControl
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class GisMapControl : UserControl
    {
        private bool IsInDesignMode
        {
            get { return (bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue; }
        }

        public GisMapControl()
        {
            InitializeComponent();

            if (IsInDesignMode) return;

            GisMap.MinZoom = 2;  //最小缩放
            GisMap.MaxZoom = 17; //最大缩放
            GisMap.Zoom = 10;     //当前缩放
            GisMap.ShowCenter = false; //不显示中心十字点
            GisMap.DragButton = MouseButton.Left; //左键拖拽地图
            GisMap.Position = new PointLatLng(30.67, 104.06); 

            GisMap.MouseLeftButtonDown += new MouseButtonEventHandler(mapControl_MouseLeftButtonDown);
            GisMap.Manager.Mode = AccessMode.ServerOnly;
            GisMap.MapProvider = GaodeMapProvider.Instance;
        }

        private void GaodeMap_Click(object sender, RoutedEventArgs e)
        {
            GisMap.Manager.Mode = AccessMode.ServerOnly;
            //try
            //{
            //    System.Net.IPHostEntry ei = System.Net.Dns.GetHostEntry("http://ditu.amap.com/");
            //    MainMap.Manager.Mode = AccessMode.ServerOnly;
            //}
            //catch(Exception ex)
            //{
            //    MainMap.Manager.Mode = AccessMode.CacheOnly;
            //    MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButton.OK, MessageBoxImage.Warning);
            //}

            GisMap.MapProvider = GaodeMapProvider.Instance;
            GisMap.Position = new PointLatLng(GisMap.Position.Lat + 11.865, GisMap.Position.Lng + 41.895);
        }

        private void BaiduMap_Click(object sender, RoutedEventArgs e)
        {
            GisMap.Manager.Mode = AccessMode.ServerOnly;
            //try
            //{
            //    System.Net.IPHostEntry ei = System.Net.Dns.GetHostEntry("http://ditu.amap.com/");
            //    MainMap.Manager.Mode = AccessMode.ServerOnly;
            //}
            //catch(Exception ex)
            //{
            //    MainMap.Manager.Mode = AccessMode.CacheOnly;
            //    MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButton.OK, MessageBoxImage.Warning);
            //}

            GisMap.MapProvider = BaiduMapProvider.Instance;
            GisMap.Position = new PointLatLng(GisMap.Position.Lat - 11.865, GisMap.Position.Lng - 41.895);
        }

        private void mapControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point clickPoint = e.GetPosition(GisMap);
            PointLatLng point = GisMap.FromLocalToLatLng((int)clickPoint.X, (int)clickPoint.Y);
            GMapMarker marker = new GMapMarker(point);
            marker.Shape = new SurveillanceCameraMarker(GisMap, marker, "测试摄像机" + (GisMap.Markers.Count + 1));
            GisMap.Markers.Add(marker);
        }
    }
}
